package com.flowerplus.store.app.photo.loader;

import android.content.Context;

import com.flowerplus.store.app.photo.loader.storage.PhotoStorage;
import com.flowerplus.store.app.photo.model.BucketImageZipModel;
import com.flowerplus.store.app.photo.model.ImageBucket;
import com.flowerplus.store.app.photo.model.ImageItem;
import com.flowerplus.store.common.data.injector.PerActivity;
import com.flowerplus.store.common.execption.utils.ExceptionHandle;
import com.flowerplus.store.common.structure.mvp.AbsBasePresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 图片加载
 */
@PerActivity
public class PhotoLoaderPresenter extends AbsBasePresenter implements PhotoLoaderContract.Presenter {
    PhotoStorage mPhotoStorage;
    Context mContext;
    PhotoLoaderContract.View mView;
    private Subscription mSubscribe;

    @Inject
    public PhotoLoaderPresenter(PhotoStorage photoStorage, Context context) {
        mPhotoStorage = photoStorage;
        mContext = context;
    }

    @Override
    public void onCreate(PhotoLoaderContract.View view) {
        mView = view;
    }

    @Override
    public void onDestroy() {
        if (mSubscribe != null) mSubscribe.unsubscribe();
        mView = null;
    }

    @Override
    public void getPhotos() {

        final List<ImageItem> imageItems = new ArrayList<>();
        final BucketImageZipModel bucketImageZipModel = new BucketImageZipModel();
        mSubscribe = Observable.create(new Observable.OnSubscribe<List<ImageBucket>>() {
            @Override
            public void call(Subscriber<? super List<ImageBucket>> subscriber) {
                subscriber.onNext(mPhotoStorage.get(mContext));
                subscriber.onCompleted();
            }
        }).flatMap(new Func1<List<ImageBucket>, Observable<ImageBucket>>() {
            @Override
            public Observable<ImageBucket> call(List<ImageBucket> imageBuckets) {
                bucketImageZipModel.setBucketImage(imageBuckets);
                return Observable.from(imageBuckets);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ImageBucket>() {
                    @Override
                    public void onCompleted() {
                        bucketImageZipModel.setImageItem(imageItems);
                        mView.loaderPhotos(bucketImageZipModel);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        String msg = ExceptionHandle.handle(e);
                        mView.error(msg);
                    }

                    @Override
                    public void onNext(ImageBucket imageBucket) {
                        imageItems.addAll(imageBucket.getImageList());
                    }

                });

    }
}
